﻿package fwk.timer.type
{
	
	import flash.events.TimerEvent;
    import flash.utils.Dictionary;
    import flash.utils.setInterval;
    import flash.utils.clearInterval;
	import fwk.timer.TimerAbstract;
	
    public class TimerSetInterval extends TimerAbstract
	{

		private var _interval:int = -1;
		private var _tickLength:int;  // en ms
		private var _callBacks:Dictionary;

		
        public function TimerSetInterval(fps:int = 60)
		{
			_fps = fps;
            _tickLength = 1000 / _fps;
				
             this._callBacks = new Dictionary();
        }

		
		override public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void 
		{
			 this._callBacks[listener] = listener;
		}
		
		override public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void 
		{
			 this._callBacks[listener] = null;
			delete this._callBacks[listener];
			

		}
		
		
		override public function start():void
		{
			if (  this._interval == -1) {
				this._interval = setInterval(this.doTick, _tickLength);
			}
		}
		override public function stop():void
		{
			if (  this._interval != -1) {
				clearInterval( this._interval);
				 this._interval = -1;
			}
		}
		
        private function doTick():void {
                for each (var callback:* in this._callBacks) callback(new TimerEvent(TimerEvent.TIMER));
        }
    }
}
